package com.cy.detectsys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cy.detectsys.entity.DbIana;
import com.cy.detectsys.entity.DetectMissionTarget;
import com.cy.detectsys.entity.DetectTarget;
import com.cy.detectsys.mapper.DetectMissionTargetMapper;
import com.cy.detectsys.mapper.DetectTargetMapper;
import com.cy.detectsys.model.PageDto;
import com.cy.detectsys.model.PageReqModel;
import com.cy.detectsys.model.vo.NetTargetVO;
import com.cy.detectsys.service.DbIanaService;
import com.cy.detectsys.mapper.DbIanaMapper;
import com.cy.detectsys.util.R;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.sql.Struct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author admin
 * @description 针对表【db_iana】的数据库操作Service实现
 * @createDate 2024-05-08 19:03:44
 */
@Service
public class DbIanaServiceImpl extends ServiceImpl<DbIanaMapper, DbIana>
        implements DbIanaService {

    @Resource
    private DetectTargetMapper targetMapper;
    @Resource
    private RestHighLevelClient restHighLevelClient;

    @Resource
    private DetectMissionTargetMapper detectMissionTargetMapper;


    @Override
    public R queryList(PageReqModel<NetTargetVO> pageModel) {

        PageHelper.startPage(pageModel.getPage(), pageModel.getPageSize());
        QueryWrapper<DetectTarget> queryWrapper = new QueryWrapper<>();


        String domain = pageModel.getModel().getDomain();
        if (StrUtil.isNotEmpty(domain)) {
            queryWrapper.like("ky_net_target_domain", domain);
        }
        String missionId = pageModel.getModel().getMissionId();

        if (StrUtil.isNotEmpty(missionId)) {
            QueryWrapper<DetectMissionTarget> detectMissionTargetQueryWrapper = new QueryWrapper<>();
            detectMissionTargetQueryWrapper.eq("ky_mission_id", missionId);
            detectMissionTargetQueryWrapper.isNotNull("ky_net_target_id");
            List<DetectMissionTarget> detectMissionTargets = detectMissionTargetMapper.selectList(detectMissionTargetQueryWrapper);
            List<String> idlist = detectMissionTargets.stream().map(item -> item.getKyNetTargetId()).collect(Collectors.toList());
            queryWrapper.in("id", idlist);
        }

        queryWrapper.isNotNull("ky_net_target_domain");

        List<DetectTarget> targets = targetMapper.selectList(queryWrapper);

        return R.ok(PageDto.listToPageDto(targets));

    }



}




